package com.cnwow.status;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class WowServerStatus {

	// 国服
	private static final String BASE_URL = "http://www.warcraftchina.com/action/system/realmStatus.aux?4316667";
	

	private static final String USER_AGENT = "Mozilla/4.5";

	private static final String data_key = "data";
	
	private static final String realmstatus_key = "realmstatus";
	
	private boolean data_ready = false;

	public boolean isData_ready() {
		return data_ready;
	}

	public JSONObject getWSSJSONObject() throws JSONException, Exception {
		return new JSONObject(getRequest(BASE_URL));
	}

	protected String getRequest(String url) throws Exception {
		return getRequest(url, new DefaultHttpClient(new BasicHttpParams()));
	}

	protected String getRequest(String url, DefaultHttpClient client)
			throws Exception {
		String result = null;
		int statusCode = 0;
		HttpGet getMethod = new HttpGet(url);
		try {
			getMethod.setHeader("User-Agent", USER_AGENT);
			// 添加用户密码验证信息
			// client.getCredentialsProvider().setCredentials(
			// new AuthScope(null, -1),
			// new UsernamePasswordCredentials(mUsername, mPassword));

			HttpResponse httpResponse = client.execute(getMethod);
			// statusCode == 200 正常
			statusCode = httpResponse.getStatusLine().getStatusCode();
			// 处理返回的httpResponse信息
			result = retrieveInputStream(httpResponse.getEntity());
		} catch (Exception e) {
			throw new Exception(e);
		} finally {
			getMethod.abort();
		}
		return result;
	}

	protected String retrieveInputStream(HttpEntity httpEntity) {
		Long l = httpEntity.getContentLength();
		int length = (int) httpEntity.getContentLength();
		// the number of bytes of the content, or a negative number if unknown.
		// If the content length is known but exceeds Long.MAX_VALUE, a negative
		// number is returned.
		// length==-1，下面这句报错，println needs a message
		if (length < 0)
			length = 10000;
		StringBuffer stringBuffer = new StringBuffer(length);
		try {
			InputStreamReader inputStreamReader = new InputStreamReader(
					httpEntity.getContent(), HTTP.UTF_8);
			char buffer[] = new char[length];
			int count;
			while ((count = inputStreamReader.read(buffer, 0, length - 1)) > 0) {
				stringBuffer.append(buffer, 0, count);
			}
		} catch (UnsupportedEncodingException e) {
		} catch (IllegalStateException e) {
		} catch (IOException e) {
		}
		return stringBuffer.toString();
	}

	protected List<?> getWowServerStatus() {

		List<Object> list = new ArrayList<Object>();
		JSONObject jsonData = null;
		JSONArray jsonRealmstatus = null;
		JSONObject detail_temple = null;
		Map<String, String> map = null;
		try {
			jsonData = (JSONObject) getWSSJSONObject().get(data_key);
			jsonRealmstatus = (JSONArray)jsonData.get(realmstatus_key);
			
			for (int index = 0; index < jsonRealmstatus.length(); index++) {
				map = new HashMap<String, String>();
				detail_temple = (JSONObject)jsonRealmstatus.get(index);
				map.put("l", detail_temple.getString("l"));
				map.put("n", detail_temple.getString("n"));
				//map.put("q", detail_temple.getString("q"));
				//map.put("s", detail_temple.getString("s"));
				map.put("t", detail_temple.getString("t"));
				// 图片
				map.put("s_img", detail_temple.getString("s"));
				map.put("l_img", detail_temple.getString("l"));
				list.add(map);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		data_ready = true;
		return list;
	}
	
	public String formatTime(long time) {
		String t = "";
		
		return t;
	}
}
